package com.neuedu.myapplication;

import org.json.JSONException;
import org.json.JSONTokener;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * @ClassName WeatherOkHttp
 * @Author name
 * @Date 2025/4/8
 * Description
 */
public class WeatherOkHttp {
    public static final String baseUrl = "http://124.93.196.45:10001";


    public static String login = baseUrl + "/dev-api/login";
    public static String weatherList = baseUrl + "/dev-api/bs-weather-report/weather/queryLastSevenDaysData/";

    public static String weatherarea = baseUrl + "/dev-api/bs-weather-report/weather/getCurrentDayData/";

    private static OkHttpClient mOkHttpClient;
    //设计模式，单例模式
    public WeatherOkHttp(OkHttpClient okHttpClient) {
        if (mOkHttpClient == null) {
            synchronized (WeatherOkHttp.class) {
                if (mOkHttpClient == null) {
                    if (okHttpClient == null) {
                        mOkHttpClient = new OkHttpClient();
                    } else {
                        mOkHttpClient = okHttpClient;
                    }
                }
            }
        }
    }

    public static Object getSuccess(Response response){
        ResponseBody responseBody = response.body();
        String responseBodyStr = "";
        Object result;
        try {
            responseBodyStr = responseBody.string();
        } catch (IOException e) {

        }
        try {
            result = new JSONTokener(responseBodyStr).nextValue();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
